--------------------- ******************************* NTO_SW Dati  STORICI  (SOLO 12 LETTURE)
select * from (
select distinct
--CODICE_CONTRATTO, STATO_CONTRATTO, MOVIMENTO, periodo,
'SNS' "CodFlusso",
 "PIvaUtente",
  "PIvaDistributore "  "PIvaDistributore",
 "CodContrDisp",
  "Pod",
 "DataMisura",
 "TipoDato",
"Validato " "Validato",
"EaM "  "EaM",
 "ErM",
"PotM",
 "EaF1",
"EaF2",
 "EaF3",
 "ErF1",
 "ErF2",
"ErF3 " "ErF3",
"PotF1",
"PotF2",
 "PotF3" 



from 
(select A.* from asm_delib_6_12_002 A,(
select "Pod",max(RN) rnmax,max(RN)-11 rnmin12 ,max(RN)-min(RN) dif from asm_delib_6_12_002 
                          --WHERE "Pod" = 'IT018E00042311'
                           group by "Pod") B
WHERE  b."Pod"=a."Pod"
AND
(DIF <=11 OR( A.rn between rnmin12 and rnmax)))

--where 
--stato_contratto <> 'W'

  
 
 )
--where "Pod"like 'IT018E00017771'
order by "CodContrDisp","Pod",to_date("DataMisura",'dd/mm/yyyy')

